#include "stdafx.h"

Map::Map()
{
	bk = ImageMgr::getInstance()->get("image/map/map_2.png");
}

Map::~Map()
{
}

void Map::setImage(const std::string & filename)
{
	img = ImageMgr::getInstance()->get(filename);

	width = img->getWidth();
	height = img->getHeight();
}

bool Map::render()
{
	//img->render(0, 0, displayX, displayY, 1920, 1080);
	bk->render(0, 0, 1920, 1080);
	img->render(-displayX, -displayY);
	//img->render(0, 0, displayX, displayY, 1920, 1080);

	return true;
}

void Map::setCollideModel(const std::string & filename, int columes, int rows)
{
	collideModel = new int*[rows];

	std::ifstream file(filename);

	for (int i = 0; i < rows; i++)
	{
		collideModel[i] = new int[columes];

		std::string line;

		std::getline(file, line);

		for (int j = 0; j < columes; j++)
		{
			collideModel[i][j] = line[j] - '0';
		}
	}
}

bool Map::collide(int x, int y, double radius)
{
	int left = (x - radius) / gridSize;
	int right = (x + radius) / gridSize;
	int top = (y - radius) / gridSize;
	int bottom = (y + radius) / gridSize;

	if (left < 0 || right >= 150 || top < 0 || bottom >= 100)
	{
		return true;
	}

	for (int i = left; i <= right; i++)
	{
		for (int j = top; j <= bottom; j++)
		{
			if (collideModel[j][i] == 0)
			{
				return true;
			}
		}
	}

	return false;
}